package main

import (
	"fmt"
	"github.com/casbin/casbin/v2"
	"log"
	"time"
)

type Person struct {
	Role string
	Name string
}

type Gate struct {
	Name string
}

type Env struct {
	Time time.Time
	Location string
}

func (env *Env) IsSchooltime() bool {
	return env.Time.Hour() >=8 && env.Time.Hour() <= 18
}


func TestTeacherEnterSchoolGate() {
	p1 := Person{Role: "Student", Name: "Yun"}
	p2 := Person{Role: "Teacher", Name: "Devin"}
	persons := []Person{p1, p2}
	g1 := Gate{Name: "School Gate"}
	g2 := Gate{Name: "Factory Gate"}
	gates := []Gate{g1, g2}

	e, err := casbin.NewEnforcer("./model.conf", "./policy.csv")
	if err != nil {
		log.Fatalf("NewEnforecer failed:%v\n", err)
	}
	envs := []*Env{InitEnv(9), InitEnv(23)}
	for _, env := range envs {
		fmt.Println("\r\nTime:",env.Time.Local())
		for _, p := range persons {
			for _, g := range gates {
				pass, _ := e.Enforce(p, g, "In", env)
				fmt.Println(p.Role, p.Name, "In", g.Name, pass)
				pass, _ = e.Enforce(p, g, "Control", env)
				fmt.Println(p.Role,p.Name, "Control", g.Name, pass)
			}
		}
	}
}

func InitEnv(hour int) *Env{
	env:=&Env{}
	env.Time=time.Date(2019,8,20,hour,0,0,0,time.Local)
	return env
}




func main() {
	TestTeacherEnterSchoolGate()
}
